import { RtcDefaultApi } from '@/unit/axios';
import { defaultErrorHandler } from '@/unit/error-handler';
import { success } from '@/unit/message';
import { action, computed, makeObservable, observable } from 'mobx';
import { singleton } from 'tsyringe';
import { LiveType } from '../home/types';

@singleton()
export class Live {
  @observable public isLoading = false;

  @observable public socket: Taro.SocketTask | undefined;

  @observable public live: LiveType = new LiveType();

  @observable public visible = false;

  @action public create = async () => {
    try {
      await RtcDefaultApi.apiLiveCreatePost({
        ...this.live,
      });

      success('创建成功');
    } catch (error) {
      console.error(error);
      defaultErrorHandler(error);
    }
  };

  @action public setVisibility = (value: boolean) => {
    this.visible = value;
  };

  @action public setLive = (value: LiveType) => {
    this.live = value;
  };

  @action public setTitle = (value: string) => {
    this.live.title = value;
  };

  @action public setExpan = (value: string) => {
    this.live.explain = value;
  };

  @computed get getDmoe() {
    return this.isLoading;
  }

  constructor() {
    makeObservable(this);
  }
}
